//
//  PassagewayInfoCell.m
//  efairy
//
//  Created by 李锋 on 2019/1/18.
//  Copyright © 2019年 East. All rights reserved.
//

#import "PassagewayInfoCell.h"
#import "DeviceListModel.h"
#import "GlobalHeader.h"



@interface PassagewayInfoCell ()<UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray<UIView *> *uIViewArray;
@end

@implementation PassagewayInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        _uIViewArray = [[NSMutableArray alloc]init];
        _textFileArray = [[NSMutableArray alloc]init];
        _textFileValue = [[NSMutableArray alloc]init];
        [self initView];
    }
    return self;
}


-(void)initView{
//    @property (nonatomic, assign) NSInteger cid; // 通道号
//    @property (nonatomic, copy) NSString *c_name; // 通道名称
//    @property (nonatomic, copy) NSString *c_value;// 通道数值
//    @property (nonatomic, assign) NSInteger c_alarm_id;// 通道状态，0-离线 1-火警 2-预警 3-故障 4-启动 5-屏蔽 6-正常

    
    _passageway = [[UIView alloc]init];//设备图表
    UIImageView *passagewayImage= [[UIImageView alloc]init];
    passagewayImage.image = [UIImage imageNamed:@"icon_label"];
    UILabel *passagewayTitle = [[UILabel alloc]init];
    passagewayTitle.font = [UIFont boldSystemFontOfSize:16.f];
    passagewayTitle.textColor = ColorFromRGB(0x333333);
    passagewayTitle.text = @"通道数据";
    
    _cancelView = [[UIView alloc]init];
    UIImageView *cancelImg = [[UIImageView alloc]init];
    cancelImg.image = [UIImage imageNamed:@"icon_cancel"];
    UILabel *cancelLable = [[UILabel alloc]init];
    cancelLable.text = @"取消";
    cancelLable.font = [UIFont boldSystemFontOfSize:14];
    cancelLable.textColor = ColorFromRGB(0x282449);
    [_cancelView sd_addSubviews:@[cancelImg,cancelLable]];
    
    cancelImg.sd_layout.heightIs(13).widthIs(13)
    .rightSpaceToView(_cancelView, 35)
    .topSpaceToView(_cancelView, 16);
    
    cancelLable.sd_layout.heightIs(14).widthIs(29)
    .rightSpaceToView(_cancelView, 0)
    .topSpaceToView(_cancelView, 15.5);
    
    
    
    _submitView = [[UIView alloc]init];
    UIImageView *submitlImg = [[UIImageView alloc]init];
    submitlImg.image = [UIImage imageNamed:@"icon_submit"];
    UILabel *submitlLable = [[UILabel alloc]init];
    submitlLable.text = @"提交";
    submitlLable.font = [UIFont boldSystemFontOfSize:14];
    submitlLable.textColor = ColorFromRGB(0x282449);
    [_submitView sd_addSubviews:@[submitlImg,submitlLable]];
    
    submitlImg.sd_layout.heightIs(13).widthIs(18)
    .rightSpaceToView(_submitView,35)
    .topSpaceToView(_submitView, 16);
    
    submitlLable.sd_layout.heightIs(14).widthIs(29)
    .rightSpaceToView(_submitView, 0)
    .topSpaceToView(_submitView, 15.5);
    
    _editView= [[UIView alloc]init];
    UIImageView *editlImg = [[UIImageView alloc]init];
    editlImg.image = [UIImage imageNamed:@"icon_editor_b"];
    UILabel *editLable = [[UILabel alloc]init];
    editLable.text = @"编辑";
    editLable.font = [UIFont boldSystemFontOfSize:14];
    editLable.textColor = ColorFromRGB(0x282449);
    [_editView sd_addSubviews:@[editlImg,editLable]];
    
    editlImg.sd_layout.heightIs(13).widthIs(13)
    .rightSpaceToView(_editView,35)
    .topSpaceToView(_editView, 16);
    
    editLable.sd_layout.heightIs(14).widthIs(29)
    .rightSpaceToView(_editView, 0)
    .topSpaceToView(_editView, 15.5);
    
    [_passageway sd_addSubviews:@[passagewayImage,passagewayTitle,_cancelView,_submitView,_editView]];
    
    passagewayImage.sd_layout
    .widthIs(6)
    .heightIs(22)
    .topSpaceToView(_passageway, 13)
    .leftSpaceToView(_passageway, 0);
    
    passagewayTitle.sd_layout
    .widthIs(100)
    .heightIs(16)
    .topSpaceToView(_passageway, 15)
    .leftSpaceToView(passagewayImage, 6);
    
    _cancelView.sd_layout.heightIs(45).widthIs(60)
    .topSpaceToView(_passageway, 0)
    .rightSpaceToView(_passageway, 12);
    
    _submitView.sd_layout.heightIs(45).widthIs(60)
    .topSpaceToView(_passageway, 0)
    .rightSpaceToView(_cancelView, 25);
    
    _editView.sd_layout.heightIs(45).widthIs(60)
    .topSpaceToView(_passageway, 0)
    .rightSpaceToView(_passageway, 12);
    
    [self.contentView addSubview:_passageway];
    
    
    _cancelView.hidden = YES;
    _submitView.hidden = YES;
    
    UITapGestureRecognizer *tapEdit=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickEdit:)];
    [_editView addGestureRecognizer:tapEdit];
    
    UITapGestureRecognizer *tapCancel=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickCancel:)];
    [_cancelView addGestureRecognizer:tapCancel];
    
    UITapGestureRecognizer *tapSubmut = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickSubmit:)];
    [_submitView addGestureRecognizer:tapSubmut];
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    if ([textField isEqual:_pName]) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (textField.text.length >= 10) {
            textField.text = [textField.text substringToIndex:10];
            return NO;
        }
//    }
    return YES;
}

-(void)setModeDatas:(PassagewayInfo *)data{
    if(data){
        _pName.text = data.c_name;
        _pValue.text = data.c_value;
    }
}


-(void)passagewayEditYES:(BOOL)b andIsSave:(BOOL)isSave{
    for(int i = 0; i < _textFileArray.count;i++){
        _textFileArray[i].enabled = b;
        if(!b && !isSave){
            _textFileArray[i].text = _textFileValue[i];
        }
    }
    if(b){
        [_textFileArray[0] becomeFirstResponder];//获取焦点
    }
}

-(void)onClickEdit:(id)tap{
    NSLog(@"点击了编辑");
    _editView.hidden= YES;
    _cancelView.hidden = NO;
    _submitView.hidden = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveIsSuccess:) name:EFAGlobalNotiPassageway object:nil];
    [self passagewayEditYES:YES andIsSave:NO];
    
}


- (void)saveIsSuccess:(NSNotification *)notification {
    NSString *isok = notification.object;
    if([isok isEqualToString:@"fail"]){
        for(int i = 0; i < _textFileArray.count;i++){
             _textFileArray[i].text = _textFileValue[i];
        }
    }else{
        for(int i = 0; i < _textFileArray.count;i++){
            _textFileValue[i] = _textFileArray[i].text;
        }
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:EFAGlobalNotiPassageway object:nil];
}

-(void)onClickCancel:(id)tap{
     NSLog(@"点击了取消");
    _editView.hidden= NO;
    _cancelView.hidden = YES;
    _submitView.hidden = YES;
     [[NSNotificationCenter defaultCenter] removeObserver:self name:EFAGlobalNotiPassageway object:nil];
     [self passagewayEditYES:NO andIsSave:NO];
}

-(void)onClickSubmit:(id)tap{
     NSLog(@"点击了提交");
    _editView.hidden= NO;
    _cancelView.hidden = YES;
    _submitView.hidden = YES;
     [self passagewayEditYES:NO andIsSave:YES];
    if(self.btnDelegate){
         [self.btnDelegate cellSubmitClicked:_textFileArray];
    }
    
}

-(void)setModeAllDatas:(NSMutableArray<PassagewayInfo *>*)data{
    if(data && data.count >0){
        for(int i = 0; i < data.count;i++){
            UIView *view = [[UIView alloc]init];
            
            UITextField *name = [[UITextField alloc]init];
            name.font = [UIFont systemFontOfSize:13.f];
            name.textColor = ColorFromRGB(0x282449);
            name.text =data[i].c_name;
            name.enabled = NO;
            
            [_textFileArray addObject:name];
            [_textFileValue addObject:data[i].c_name];
            
            UILabel *value = [[UILabel alloc]init];
            value.font = [UIFont systemFontOfSize:13.f];
            value.textColor = ColorFromRGB(0x999999);
            value.text = data[i].c_value;
            value.textAlignment = NSTextAlignmentRight;
            
            
            if(i < data.count-1){
                UIView *line = [[UIView alloc]init];
                line.backgroundColor = ColorFromRGB(0xE5E5E5);
                [view sd_addSubviews:@[name,value,line]];
                line.sd_layout.heightIs(0.5)
                .topSpaceToView(view, 40)
                .leftSpaceToView(view, 11)
                .rightSpaceToView(view, 0);
            }else{
                 [view sd_addSubviews:@[name,value]];
            }
            
            name.sd_layout.widthIs(180)
            .heightIs(40)
            .leftSpaceToView(view, 12);
            
            value.sd_layout.heightIs(13)
            .topSpaceToView(view, 13.5)
            .leftSpaceToView(name, 6)
            .rightSpaceToView(view,12);
            name.delegate = self;
            [self.contentView addSubview:view];
            [_uIViewArray addObject:view];
        }
        _passageway.sd_layout.heightIs(45)
        .widthIs(SCREEN_WIDTH)
        .topSpaceToView(self.contentView, 0);
        
        for(int i = 0; i < data.count;i++){
            if(i == 0){
                _uIViewArray[i].sd_layout.heightIs(40)
                .widthIs(SCREEN_WIDTH)
                .topSpaceToView(_passageway, 0);
            }else {
                _uIViewArray[i].sd_layout.heightIs(40)
                .widthIs(SCREEN_WIDTH)
                .topSpaceToView(_uIViewArray[i-1], 0);
            }
        }
        [self setupAutoHeightWithBottomView:_uIViewArray[_uIViewArray.count-1] bottomMargin:0];
    }
}
@end
